package com.tanhua.dubbo.Api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author 刘付磊
 * @date 2021/11/15 0015
 */
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //1.根据toUserId查询，根据评分score排序，取第一条

        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);

        //调用mongoTemplate

        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public PageResult queryRecommendUserDto(Integer page, Integer pagesize, Long toUserId) {
        //1.构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2.构建Query对象
        Query query = Query.query(criteria);
        //3.计算总数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //4.查询数据列表
        query.with(Sort.by("score")).limit(pagesize).skip((page-1)*pagesize);
        //5.构建返回值
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        PageResult pr = new PageResult(page,pagesize, (int) count,recommendUsers);
        return pr;
    }

    @Override
    public RecommendUser queryByUserId(Long userId, Long id) {
        //1.构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(id).and("userId").is(userId);
        //2.构建Query对象
        Query query = Query.query(criteria);
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        if(user == null){
            user = new RecommendUser();
            user.setUserId(userId);
            user.setToUserId(id);
            //构建缘分值
            user.setScore(95d);
        }
        return user;
    }



    //1.排除喜欢和不喜欢的用户
    //2.随机展示数据列表
    //3.指定数量
    //4.构造返回
    @Override
    public List<RecommendUser> cards(Long userId, int counts) {
        //1.查询喜欢 不喜欢的用户id
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query  =new Query(criteria);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserId = CollUtil.getFieldValues(userLikeList, "likeUserId", Long.class);

        //2.构造查询用户条件
        Criteria nin = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserId);
        //3.使用统计函数，随机获取推荐的用户列表
        TypedAggregation<RecommendUser> aggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(nin),//指定查询条件
                Aggregation.sample(counts)
        );
        AggregationResults<RecommendUser> users = mongoTemplate.aggregate(aggregation, RecommendUser.class);
        //4.构造返回

        return users.getMappedResults();
    }
}
